package com.fare.taluo.action.index;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fare.taluo.BaseFdcAction;
import com.fare.taluo.application.ChannelTariffsApplication;
import com.fare.taluo.application.plane.city.PortApplication;
import com.fare.taluo.application.plane.mycare.UserCareLineApplication;
import com.fare.taluo.auth.security.LogonUserCertificate;
import com.fare.taluo.util.DateUtils;
import com.fare.taluo.util.I18nUtil;
import com.fare.taluo.util.XmlUtil;
import com.fare.taluo.vo.core.ChannelPlaneinfoByView;
import com.fare.taluo.vo.core.CityEntityVO;
import com.fare.taluo.vo.core.CplaneInfoByTopFive;
import com.fare.taluo.vo.core.HomeChannelCompare;
import com.fare.taluo.vo.core.PortEntityVO;
import com.fare.taluo.vo.core.UserCareLineEntityVO;

/**
 * @author xiaoshuwen
 * 渠道首页 Action
 */
@Controller("channelTariffsAction")
@Scope("prototype")
public class ChannelTariffsAction extends BaseFdcAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4696305168199962886L;
	{
		Locale locale = super.getLocale();
		one=I18nUtil.getProperties(locale).get("channelPage.monday");
		two=I18nUtil.getProperties(locale).get("channelPage.tuesday");
		three=I18nUtil.getProperties(locale).get("channelPage.wednesday");
		four=I18nUtil.getProperties(locale).get("channelPage.thursday");
		five=I18nUtil.getProperties(locale).get("channelPage.friday");
		six=I18nUtil.getProperties(locale).get("channelPage.saturday");
		seven=I18nUtil.getProperties(locale).get("channelPage.sunday");
		RetailPricing=I18nUtil.getProperties(locale).get("channelPage.RetailPricing");
		OfficialWebsite=I18nUtil.getProperties(locale).get("channelPage.OfficialWebsite");
	}
	private String one;
	private String two;
	private String three;
	private String four;
	private String five;
	private String six;
	private String seven;
	private String RetailPricing;
	private String OfficialWebsite;
	
	@Resource(name = "channelTariffsApplication")
	private ChannelTariffsApplication channelTariffsApplication;
	@Resource(name = "portApplication")
	private PortApplication portApplication;
	@Resource(name = "userCareLineApplication")
	private UserCareLineApplication userCareLineApplication;

	public ChannelTariffsApplication getChannelTariffsApplication() {
		return channelTariffsApplication;
	}

	public void setChannelTariffsApplication(
			ChannelTariffsApplication channelTariffsApplication) {
		this.channelTariffsApplication = channelTariffsApplication;
	}

	private String sid;// 区域编码
	private String sope_Time;// 下拉框时间范围选择
	private List<String> listDate = new ArrayList<String>();// 日期集合
	private List<String> list_str = new ArrayList<String>();//日期轴
    private String isnull;//是否为空
	private String fromCity;//出发城市
	private String toCity;//目的地城市
	private String idToaClass;
	private String fromCityStr;//出发城市/机场三字码
	private String toCityStr;//目的城市/机场三字码
	private int isCity;//标识是否为城市或者机场
	
	private List<String> fromCode=new ArrayList<String>();//出发机场三字码
	private List<String> toCode=new ArrayList<String>();//目的地机场三字码
	
	private List<ChannelPlaneinfoByView> cpls = new ArrayList<ChannelPlaneinfoByView>();//周价差TOp3
	private String hdate;
	private List<CplaneInfoByTopFive> listTobFive = new ArrayList<CplaneInfoByTopFive>();// 前五名价差比相关显示

    private Map<String,Integer> nowDatemap = new HashMap<String,Integer>();//去哪儿网昨日本日与官网差价比监控
    private Map<String,Integer> nextmap = new HashMap<String,Integer>();//去哪儿淘宝网昨日本日与官网差价比监控
    private Map<String,Integer> nextmap3 = new HashMap<String,Integer>();//去哪儿淘宝网携程艺龙未来3天与官网差价比监控
    private Map<String,Integer> nextmap7 = new HashMap<String,Integer>();//去哪儿淘宝网携程艺龙未来7天与官网差价比监控
    private Map<String,Integer> nextmap15 = new HashMap<String,Integer>();//去哪儿淘宝网携程艺龙未来15天与官网差价比监控
    private List<HomeChannelCompare> hlist =null;
    
    private List<UserCareLineEntityVO> citylist = new ArrayList<UserCareLineEntityVO>();
    
    
	public List<UserCareLineEntityVO> getCitylist() {
		return citylist;
	}

	public void setListDate(List<String> listDate) {
		this.listDate = listDate;
	}

	public String getSope_Time() {
		return sope_Time;
	}

	public void setSope_Time(String sope_Time) {
		this.sope_Time = sope_Time;
	}

	public List<String> getListDate() {
		return listDate;
	}

	public List<String> getList_str() {
		return list_str;
	}

	public void setList_str(List<String> list_str) {
		this.list_str = list_str;
	}

	private String xml;

	public String getXml() {
		return xml;
	}

	public void setXml(String xml) {
		this.xml = xml;
	}
   
	public Map<String, Integer> getNowDatemap() {
		return nowDatemap;
	}

	public void setNowDatemap(Map<String, Integer> nowDatemap) {
		this.nowDatemap = nowDatemap;
	}

	public Map<String, Integer> getNextmap() {
		return nextmap;
	}

	public void setNextmap(Map<String, Integer> nextmap) {
		this.nextmap = nextmap;
	}

	
	public Map<String, Integer> getNextmap3() {
		return nextmap3;
	}

	public void setNextmap3(Map<String, Integer> nextmap3) {
		this.nextmap3 = nextmap3;
	}

	public Map<String, Integer> getNextmap7() {
		return nextmap7;
	}

	public void setNextmap7(Map<String, Integer> nextmap7) {
		this.nextmap7 = nextmap7;
	}

	public Map<String, Integer> getNextmap15() {
		return nextmap15;
	}

	public void setNextmap15(Map<String, Integer> nextmap15) {
		this.nextmap15 = nextmap15;
	}


	public List<CplaneInfoByTopFive> getListTobFive() {
		return listTobFive;
	}

	public void setListTobFive(List<CplaneInfoByTopFive> listTobFive) {
		this.listTobFive = listTobFive;
	}


	public String getHdate() {
		return hdate;
	}

	public void setHdate(String hdate) {
		this.hdate = hdate;
	}

	public PortApplication getPortApplication() {
		return portApplication;
	}

	public void setPortApplication(PortApplication portApplication) {
		this.portApplication = portApplication;
	}

	public String getFromCity() {
		return fromCity;
	}

	public void setFromCity(String fromCity) {
		this.fromCity = fromCity;
	}

	public String getToCity() {
		return toCity;
	}

	public void setToCity(String toCity) {
		this.toCity = toCity;
	}

	public List<ChannelPlaneinfoByView> getCpls() {
		return cpls;
	}

	public void setCpls(List<ChannelPlaneinfoByView> cpls) {
		this.cpls = cpls;
	}

	private String prid;

	public String getPrid() {
		return prid;
	}

	public void setPrid(String prid) {
		this.prid = prid;
	}
	public String getIsnull() {
		return isnull;
	}

	public void setIsnull(String isnull) {
		this.isnull = isnull;
	}
	public String getIdToaClass() {
		return idToaClass;
	}

	public void setIdToaClass(String idToaClass) {
		this.idToaClass = idToaClass;
	}
	
	public List<HomeChannelCompare> getHlist() {
		return hlist;
	}

	public void setHlist(List<HomeChannelCompare> hlist) {
		this.hlist = hlist;
	}
	
	public int getIsCity() {
		return isCity;
	}

	public void setIsCity(int isCity) {
		this.isCity = isCity;
	}

	public String getFromCityStr() {
		return fromCityStr;
	}

	public void setFromCityStr(String fromCityStr) {
		this.fromCityStr = fromCityStr;
	}

	public String getToCityStr() {
		return toCityStr;
	}

	public void setToCityStr(String toCityStr) {
		this.toCityStr = toCityStr;
	}

	/**
	 * 定位时间轴
	 * @throws ParseException
	 */
	public void getChioceDateByshow() throws ParseException{
		DateFormat df = new SimpleDateFormat("MM/dd");
		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if(StringUtils.isBlank(this.sope_Time)){
			Date nowDate = new Date();
			this.sope_Time=sdf.format(nowDate);
		}
		Calendar c = Calendar.getInstance();
		
		List<Date> listdate = this.getSevDayByChioce(this.sope_Time);
		for (int i = 0; i < listdate.size(); i++) {
			/*if(listdate.get(i).getDay()==0){
				this.list_str.add(df.format(listdate.get(i)) + "-周"+
						"日");
			}*/
			c.setTime(listdate.get(i));
			switch(c.get(Calendar.DAY_OF_WEEK)){
			case 1 :this.list_str.add(df.format(listdate.get(i)) + "-"+seven);break;
			case 2 :this.list_str.add(df.format(listdate.get(i)) + "-"+one);break;	
			case 3 :this.list_str.add(df.format(listdate.get(i)) + "-"+two);break;	
			case 4 :this.list_str.add(df.format(listdate.get(i)) + "-"+three);break;	
			case 5 :this.list_str.add(df.format(listdate.get(i)) + "-"+four);break;	
			case 6 :this.list_str.add(df.format(listdate.get(i)) + "-"+five);break;	
			case 7 :this.list_str.add(df.format(listdate.get(i)) + "-"+six);break;	
				}
		}
//		JsonUtil.toJSONOut(JSONArray.fromObject(this.list_str).toString());
	}
	/**
	 * AJAX获取日期轴
	 */
	public void getDateList() {
		DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
		List<Date> list = getDateList(30);
		Date d = null;
		Date d2 = null;
		for (int i = 0; i < list.size(); i = i + 6) {
			d = list.get(i);
			if (i + 6 >= list.size()) {
				d2 = list.get(list.size() - 1);
			} else {
				d2 = list.get(i + 6);
			}
			try {
				this.listDate.add(df.format(d) + "--" + df.format(d2));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		this.getWeekDate();

		super.toJSONOut(JSONArray.fromObject(this.list_str).toString());
	}
	
	/**
	 * 进入渠道首页默认加载的数据
	 * @return
	 * @throws Exception
	 */
	public String toCtJsp() throws Exception {
		LogonUserCertificate user = com.fare.taluo.auth.security.ContextLoginUser.getLoginUser();// /(User)super.getSession().get("currentUser");
		citylist = userCareLineApplication.findCareLineVO(user.getLocalUserId());
		Locale locale = super.getLocale();
		/*for(UserCareLineEntityVO careLine:citylist){
			if(careLine.getArrivalcity().indexOf("(")>0){
				careLine.setArrivalcity(careLine.getArrivalcity().substring(0, careLine.getArrivalcity().indexOf("(")));
			}
			if(careLine.getDepartcity().indexOf("(")>0){
				careLine.setDepartcity(careLine.getDepartcity().substring(0, careLine.getDepartcity().indexOf("(")));
			}
			careLine.setArrivalcity(null==I18nUtil.getProperties(locale).get(careLine.getArrivalcity())?careLine.getArrivalcity():
				I18nUtil.getProperties(locale).get(careLine.getArrivalcity()));
			careLine.setDepartcity(null==I18nUtil.getProperties(locale).get(careLine.getDepartcity())?careLine.getDepartcity():
				I18nUtil.getProperties(locale).get(careLine.getDepartcity()));
		}*/
		//国际化改成三字码
		I18nUtil.cityCodeI18N(citylist, locale);
		this.returnPageURL = "/jsp/channel_index.jsp";
		request.setAttribute("maxDate", DateUtils.getDateAfter(29));//目前只支持30天的数据 
		return "dispatcher";
	}
	/**
	 * 周价差7天(含当天)		周差价top3
	 * @throws Exception 
	 */
	public void WeekSpreadTop3() throws Exception{
		Date now = new Date();
		Locale locale = super.getLocale();
		DateFormat dfn = new SimpleDateFormat("yyyyMMdd");
		NumberFormat numberFormat = NumberFormat.getInstance();
		// 设置精确到小数点后1位
		numberFormat.setMaximumFractionDigits(1);
		LogonUserCertificate user = com.fare.taluo.auth.security.ContextLoginUser.getLoginUser();
		Calendar c = Calendar.getInstance();
		c.setTime(now);
		loadFromCityStr();
		loadToCityStr();
		List<HomeChannelCompare> hlist=null;
		if(fromCode.size()<=0||fromCode==null||toCode.size()<=0||toCode==null){
			hlist =this.channelTariffsApplication.getPlaneInfoByRowKeySeven(dfn.format(now),dfn.format(now),user.getSourceKey(),null,null);
			for(int i=7;i>0;i--){
				c.set(Calendar.DATE, c.get(Calendar.DATE) - 1 );
				Date endDateByweek = c.getTime();
				hlist.addAll(0,this.channelTariffsApplication.getPlaneInfoByRowKeySeven(dfn.format(endDateByweek),dfn.format(endDateByweek), user.getSourceKey(),null,null));
			}
		}else {
			for(int i=0;i<fromCode.size();i++){
				for(int j=0;j<toCode.size();j++){
					hlist =this.channelTariffsApplication.getPlaneInfoByRowKeySeven(dfn.format(now),dfn.format(now),user.getSourceKey(),fromCode.get(i),toCode.get(j));
					for(int m=7;m>0;m--){
						c.set(Calendar.DATE, c.get(Calendar.DATE) - 1 );
						Date endDateByweek = c.getTime();
						hlist.addAll(0,this.channelTariffsApplication.getPlaneInfoByRowKeySeven(dfn.format(endDateByweek),dfn.format(endDateByweek),user.getSourceKey(),fromCode.get(i),toCode.get(j)));
					}
					hlist.addAll(hlist);
				}
			}
		}
		
		for (HomeChannelCompare h : hlist.size() >= 3 ? hlist.subList(0, 3)
				: hlist.subList(0, hlist.size())) {
			ChannelPlaneinfoByView cv = new ChannelPlaneinfoByView();
			cv.setHb(h.getFlightBatch());
			cv.setJcb(numberFormat.format(h.getComparePrice()));
			cv.setQdName(h.getAgentName());
			cv.setSdate(h.getFlightBatchDate());
			cv.setGrabDate(new SimpleDateFormat("yyyy-MM-dd").format(new SimpleDateFormat("yyyyMMddHHmmssSS").parse(h.getGrabDate())));
			cv.setGrabChannel(h.getGrabChannel());
			this.cpls.add(cv);
		}
		if(cpls.size()>0){
			I18nUtil.companyI18N(cpls, locale);
		}
		Map<String,Object> jsonData = new HashMap<String, Object>();
		jsonData.put("cpls", cpls);
		String dataStr = com.fare.taluo.util.JsonUtil.toJson(jsonData);
		super.toJSONOut(dataStr);
	}
	/**
	 * 昨天今天差价比
	 * @throws Exception 
	 */
	public void  todayThanYesterdaySpread() throws  Exception{
		Map<String,Object> jsonData = new HashMap<String, Object>();
		//未来3天的价差比超3%超6%数量监控
		LogonUserCertificate user = com.fare.taluo.auth.security.ContextLoginUser.getLoginUser();
		Date nowDate = new Date();
		DateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		loadFromCityStr();
		loadToCityStr();
		if(fromCode.size()<=0||fromCode==null||toCode.size()<=0||toCode==null){
			this.nowDatemap = this.channelTariffsApplication.getChannelPriceSpread(sdf.format(nowDate), user.getSourceKey(), "", null, null);
		}else {
			for(int i=0;i<fromCode.size();i++){
				for(int j=0;j<toCode.size();j++){
					this.nowDatemap = this.channelTariffsApplication.getChannelPriceSpread(sdf.format(nowDate), user.getSourceKey(), "", fromCode.get(i), toCode.get(j));
					this.nowDatemap.putAll(nowDatemap);
				}
			}
		}
		jsonData.put("nowDatemap", nowDatemap);
		Calendar cn = Calendar.getInstance();
		cn.setTime(nowDate);
		cn.set(Calendar.DATE, cn.get(Calendar.DATE) - 1);
		Date nowDateNext = cn.getTime();
		if(fromCode.size()<=0||fromCode==null||toCode.size()<=0||toCode==null){
			this.nextmap = this.channelTariffsApplication.getChannelPriceSpread(sdf.format(nowDateNext), user.getSourceKey(), "", null, null);
		}else {
			for(int i=0;i<fromCode.size();i++){
				for(int j=0;j<toCode.size();j++){
					this.nextmap = this.channelTariffsApplication.getChannelPriceSpread(sdf.format(nowDateNext),user.getSourceKey(), "",fromCode.get(i), toCode.get(j));
					this.nextmap.putAll(nextmap);
				}
			}
		}
		jsonData.put("nextmap", nextmap);
		String dataStr = com.fare.taluo.util.JsonUtil.toJson(jsonData);
		super.toJSONOut(dataStr);
	}
	/**
	 * 未来3天的差价比
	 * @throws Exception 
	 */
	public void futureThreeDaysSpread() throws  Exception{
		Map<String,Object> jsonData = new HashMap<String, Object>();
		//昨日本日价差比超3超6数量监控
		LogonUserCertificate user = com.fare.taluo.auth.security.ContextLoginUser.getLoginUser();
		Date nowDate = new Date();
		DateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		loadFromCityStr();
		loadToCityStr();
//		this.nowDatemap = this.channelTariffsApplication.getChannelPriceSpread(sdf.format(nowDate), user.getSourceId()+"", "", this.fromCityStr, this.toCityStr);
//		jsonData.put("nowDatemap", nowDatemap);
		//未来3天
		Calendar cn3 = Calendar.getInstance();
		cn3.setTime(nowDate);
		cn3.set(Calendar.DATE, cn3.get(Calendar.DATE) +3);/*+3表示未来3天 */  //昨天  cn.set(Calendar.DATE, cn.get(Calendar.DATE) - 1);
		Date nextmap3T = cn3.getTime();
		//获得未来3天的数据
		if(fromCode.size()<=0||fromCode==null||toCode.size()<=0||toCode==null){
			this.nextmap3 = this.channelTariffsApplication.getChannelPriceSpread(sdf.format(nextmap3T), user.getSourceKey(), "", null, null);
		}else {
			for(int i=0;i<fromCode.size();i++){
				for(int j=0;j<toCode.size();j++){
					this.nextmap3 = this.channelTariffsApplication.getChannelPriceSpread(sdf.format(nextmap3T), user.getSourceKey(), "", fromCode.get(i), toCode.get(j));
					this.nextmap3.putAll(nextmap3);
				}
			}
		}
		jsonData.put("nextmap3", nextmap3);
		
		//未来7天
		Calendar cn7 = Calendar.getInstance();
		cn7.setTime(nowDate);
		cn7.set(Calendar.DATE, cn7.get(Calendar.DATE) + 7);
		Date nextmap7T = cn7.getTime();
		//获得未来7天的数据
		if(fromCode.size()<=0||fromCode==null||toCode.size()<=0||toCode==null){
			this.nextmap7 = this.channelTariffsApplication.getChannelPriceSpread(sdf.format(nextmap7T), user.getSourceKey(), "", null, null);
		}else {
			for(int i=0;i<fromCode.size();i++){
				for(int j=0;j<toCode.size();j++){
					this.nextmap7 = this.channelTariffsApplication.getChannelPriceSpread(sdf.format(nextmap7T), user.getSourceKey(), "", fromCode.get(i), toCode.get(j));
					this.nextmap7.putAll(nextmap7);
				}
			}
		}
		jsonData.put("nextmap7", nextmap7);
		
		//未来15天的数据
		Calendar cn15 = Calendar.getInstance();
		cn15.setTime(nowDate);
		cn15.set(Calendar.DATE, cn15.get(Calendar.DATE) + 15);
		Date nextmap15T = cn15.getTime();
		//获得未来15天的数据
		if(fromCode.size()<=0||fromCode==null||toCode.size()<=0||toCode==null){
			this.nextmap15 = this.channelTariffsApplication.getChannelPriceSpread(sdf.format(nextmap15T),user.getSourceKey(), "", null, null);
		}else {
			for(int i=0;i<fromCode.size();i++){
				for(int j=0;j<toCode.size();j++){
					this.nextmap15 = this.channelTariffsApplication.getChannelPriceSpread(sdf.format(nextmap15T),user.getSourceKey(), "", fromCode.get(i), toCode.get(j));
					this.nextmap15.putAll(nextmap15);
				}
			}
		}
		jsonData.put("nextmap15", nextmap15);
		
		String dataStr = com.fare.taluo.util.JsonUtil.toJson(jsonData);
		super.toJSONOut(dataStr);
	}
	/**
	 * 加载swf数据
	 * @throws Exception
	 */
	public void loadSwfData() throws Exception{
		Locale locale = super.getLocale();
		Map<String,Object> jsonData = new HashMap<String, Object>();
		this.getChioceDateByshow();
		jsonData.put("list_str", list_str);
		LogonUserCertificate user = com.fare.taluo.auth.security.ContextLoginUser.getLoginUser();
		NumberFormat numberFormat = NumberFormat.getInstance();
		// 设置精确到小数点后1位
		numberFormat.setMaximumFractionDigits(1);
		loadFromCityStr();
		loadToCityStr();
		//查询top10价差排行		
		List<HomeChannelCompare> hcclistAll=new ArrayList<HomeChannelCompare>();
		if(fromCode.size()<=0||fromCode==null||toCode.size()<=0||toCode==null){
			List<HomeChannelCompare> hcclist = 
					this.channelTariffsApplication.getPlaneInfoByRowKey(
							(StringUtils.isBlank(this.sope_Time) ?
							DateUtils.getCurrentDateStartTime() : 
								DateUtils.strToDate(this.sope_Time, "yyyy-MM-dd")), 
								user.getSourceKey(), null,null);
			hcclistAll.addAll(hcclist);
		}else {
			for(int i=0;i<fromCode.size();i++){
				for(int j=0;j<toCode.size();j++){
					List<HomeChannelCompare> hcclist = 
							this.channelTariffsApplication.getPlaneInfoByRowKey(
									(StringUtils.isBlank(this.sope_Time) ?
											DateUtils.getCurrentDateStartTime() : 
												DateUtils.strToDate(this.sope_Time, "yyyy-MM-dd")),
												user.getSourceKey(), 
										fromCode.get(i),toCode.get(j));
					hcclistAll.addAll(hcclist);
				}
			}
		}
		
		if(request.getParameter("bySearch")!=null){
			 //top5为空则直接返回页面提示无此航线数据
			if(hcclistAll.size()==0 || hcclistAll==null){
			    this.isnull="null";
			    jsonData.put("isnull", isnull);
			}
		}
		// 航线,官网价格，渠道价格
		List<String> listhx = new ArrayList<String>();
		List<Integer> prices = new ArrayList<Integer>(10);
		List<Integer> prices2 = new ArrayList<Integer>(10);
		//为空默认添加空数据用于展示图像
		if(hcclistAll==null || hcclistAll.size()<=0){
			listhx.add(" ");
			listhx.add(" ");
			listhx.add(" ");
			listhx.add(" ");
			listhx.add(" ");
			listhx.add(" ");
			listhx.add(" ");
			listhx.add(" ");
			listhx.add(" ");
			listhx.add(" ");
			prices.add(0);
			prices2.add(0);
			prices.add(0);
			prices2.add(0);
			prices.add(0);
			prices2.add(0);
			prices.add(0);
			prices2.add(0);
			prices.add(0);
			prices2.add(0);
			prices.add(0);
			prices2.add(0);
			prices.add(0);
			prices2.add(0);
			prices.add(0);
			prices2.add(0);
			prices.add(0);
			prices2.add(0);
			prices.add(0);
			prices2.add(0);
		}
		//组装数据用于生成图像统计
		for (HomeChannelCompare h : hcclistAll) {
			CplaneInfoByTopFive ctf = new CplaneInfoByTopFive();
			ctf.setChannelName(h.getGrabChannel());
			ctf.setOwName(h.getAgentName());
			ctf.setSpreadsThan(numberFormat.format(h.getComparePrice()));
			this.listTobFive.add(ctf);
			listhx.add(h.getFlightBatch());
			prices.add(new BigDecimal(h.getChannelPrice()).setScale(0,BigDecimal.ROUND_FLOOR).intValue());
			prices2.add(new BigDecimal(h.getHomePrice()).setScale(0,BigDecimal.ROUND_FLOOR).intValue());
		}
		if(listTobFive.size()>0){
			I18nUtil.companyI18N_2(listTobFive, locale);
		}
		jsonData.put("listTobFive", listTobFive);
		prices=this.addListValue(prices, 10);
		prices2=this.addListValue(prices2, 10);
		listhx = this.addListStrValue(listhx,10);
		Map<String, List<Integer>> map = new HashMap<String, List<Integer>>();
		Map<String, List<Integer>> map2 = new HashMap<String, List<Integer>>();
		map.put(RetailPricing, prices);
		map2.put(OfficialWebsite, prices2);
		this.xml = XmlUtil.getXML(listhx, map, map2);
		jsonData.put("xml", xml);
		/**
		 * $("#hsope_Time").val(result.sope_Time);
			    	$("#isnull").val(result.isnull);
			    	$("#fcname").val(result.fromCity);
			    	$("#tcname").val(result.toCity);
			    	$("#isdefault").val(result.isdefault);
			    	$("#idToaClass").val(result.idToaClass);
		 */
		jsonData.put("sope_Time", sope_Time);
		jsonData.put("fromCity", fromCity);
		jsonData.put("toCity", toCity);
		jsonData.put("isCity", isCity);
		jsonData.put("idToaClass", idToaClass);
		String dataStr = com.fare.taluo.util.JsonUtil.toJson(jsonData);
		super.toJSONOut(dataStr);
	}
	private void loadFromCityStr(){
		List<CityEntityVO> cityEntities=new ArrayList<CityEntityVO>();
		if(isCity==1){
			cityEntities=portApplication.findCityEntityByCityCode(fromCityStr);
			if(cityEntities.size()>0){
				String cityId=String.valueOf(cityEntities.get(0).getId());
				if(StringUtils.isNotBlank(fromCityStr)){
					List<PortEntityVO> portlistList=this.portApplication.findPortEntityByCityCode(cityId);
					if(portlistList.size()>0){
						this.fromCity=portlistList.get(0).getCityName();
						for(int i=0;i<portlistList.size();i++){
							fromCode.add(portlistList.get(i).getPortCode());
						}
					}
				
				}else{
				// 返回航段
					if (StringUtils.isNotBlank(this.fromCity)&& !this.fromCity.replace(" ", "").equalsIgnoreCase("起始地") && !this.fromCity.replace(" ", "").equalsIgnoreCase("中文/拼音")) {
						PortEntityVO vo = this.portApplication.findPortEntityByCityName(this.fromCity);
						if (vo != null) {
							fromCityStr = vo.getPortCode(); 
						}
					}
				}
			}
		}else {
			if(StringUtils.isNotBlank(fromCityStr)){
				this.fromCity = this.portApplication.findCityNameByPortCode(fromCityStr).getCityName();
				fromCode.add(fromCityStr);
			}else{
				// 返回航段
				if (StringUtils.isNotBlank(this.fromCity)&& !this.fromCity.replace(" ", "").equalsIgnoreCase("起始地") && !this.fromCity.replace(" ", "").equalsIgnoreCase("中文/拼音")) {
					PortEntityVO vo = this.portApplication.findPortEntityByCityName(this.fromCity);
					if (vo != null) {
						fromCityStr = vo.getPortCode();
					}
				}
			}
		}
	}
	private void loadToCityStr(){
		List<CityEntityVO> cityEntities=new ArrayList<CityEntityVO>();
		if(isCity==1){
			cityEntities=portApplication.findCityEntityByCityCode(toCityStr);
			if(cityEntities.size()>0){
				String cityId=String.valueOf(cityEntities.get(0).getId());
				if(StringUtils.isNotBlank(toCityStr)){
					List<PortEntityVO> portlistList=this.portApplication.findPortEntityByCityCode(cityId);
					if(portlistList.size()>0){
						this.toCity=portlistList.get(0).getCityName();
						for(int i=0;i<portlistList.size();i++){
							toCode.add(portlistList.get(i).getPortCode());
						}
					}
				
				}else{
					if (StringUtils.isNotBlank(this.toCity)&& !this.toCity.replace(" ", "").equalsIgnoreCase("目的地") && !this.toCity.replace(" ", "").equalsIgnoreCase("中文/拼音")) {
						PortEntityVO vo2 = this.portApplication.findPortEntityByCityName(this.toCity);
						if (vo2 != null) {
							toCityStr = vo2.getPortCode();
						}
					}
				}
			}
		}else{
			if(StringUtils.isNotBlank(toCityStr)){
				this.toCity = this.portApplication.findCityNameByPortCode(toCityStr).getCityName();
				toCode.add(toCityStr);
			}else{
				if (StringUtils.isNotBlank(this.toCity)&& !this.toCity.replace(" ", "").equalsIgnoreCase("目的地") && !this.toCity.replace(" ", "").equalsIgnoreCase("中文/拼音")) {
					PortEntityVO vo2 = this.portApplication.findPortEntityByCityName(this.toCity);
					if (vo2 != null) {
						toCityStr = vo2.getPortCode();
					}
				}
			}
		}
		
	}
	/**
	 * 用于top5少于5条数据的情况默认添加空数据添加到5条用于显示图像与首页右边数据对应(字符串)
	 * @param list
	 * @param i
	 * @return
	 */
		private List<String> addListStrValue(List<String> list, int i){
			if(list == null) list = new ArrayList<String>(i);
			for(int j = list.size(); j < i; j++){
				list.add(" ");
			}
			return list;
		}
/**
 * 用于top5少于5条数据的情况默认添加空数据添加到5条用于显示图像与首页右边数据对应(整型)
 * @param list
 * @param i
 * @return
 */
	private List<Integer> addListValue(List<Integer> list, int i){
		if(list == null) list = new ArrayList<Integer>(i);
		for(int j = list.size(); j < i; j++){
			list.add(0);
		}
		return list;
	}

	public String getSid() {
		return sid;
	}

	public void setSid(String sid) {
		this.sid = sid;
	}

	/**
	 * 返回用户选择的日期轴(7天)
	 * @param scopeTime
	 * @return
	 * @throws ParseException
	 */
	public List<Date> getSevDayByChioce(String scopeTime) throws ParseException{
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		List<Date> list = getDateList(30);
		List<Date> listSevDay = new ArrayList<Date>();
		for(Date d : list){
			if(df.format(d).equalsIgnoreCase(scopeTime)){
				Date ds =  df.parse(scopeTime);
				Calendar c = Calendar.getInstance();
				c.setTime(ds);
				c.set(Calendar.DATE, c.get(Calendar.DATE) - 3);
				
				Date endDate = c.getTime();

				Calendar c2 = Calendar.getInstance();
				c2.setTime(ds);
				c2.set(Calendar.DATE, c2.get(Calendar.DATE) + 4);
				Date endDate2 = c2.getTime();

				int dc = (int) ((endDate2.getTime() - endDate.getTime()) / (1000 * 60 * 60 * 24));// 日期差7天

				Date ced = null;
				for (int i = 0; i < dc; i++) {
					Calendar ce = Calendar.getInstance();
					ce.setTime(endDate);
					ce.set(Calendar.DATE, ce.get(Calendar.DATE) + i);
					ced = ce.getTime();
					listSevDay.add(ced);
				}
			}
		}
		return listSevDay;
	}
	/**
	 *  返回日期范围内指定7天日期
	 * @param dayNum
	 * @return
	 */
	private static List<Date> getDateListByWeek(int dayNum) {
		int begDay = 0;
		int endDay = 0;
		// 7,13,19,25,31,37,42,45,
		if (dayNum == 7) {
			endDay = 7;
		}
		if (dayNum == 13) {
			begDay = 6;
			endDay = 13;
		}
		if (dayNum == 19) {
			begDay = 12;
			endDay = 19;
		}
		if (dayNum == 25) {
			begDay = 18;
			endDay = 25;
		}
		if (dayNum == 31) {
			begDay = 24;
			endDay = 31;
		}
		if (dayNum == 37) {
			begDay = 30;
			endDay = 37;
		}
		if (dayNum == 42) {
			begDay = 36;
			endDay = 42;
		}
		if (dayNum == 45) {
			begDay = 41;
			endDay = 45;
		}
		Date nowDate = new Date();
		Calendar c = Calendar.getInstance();
		c.setTime(nowDate);
		c.set(Calendar.DATE, c.get(Calendar.DATE) + begDay);
		Date endDate = c.getTime();

		Calendar c2 = Calendar.getInstance();
		c2.setTime(nowDate);
		c2.set(Calendar.DATE, c2.get(Calendar.DATE) + endDay);
		Date endDate2 = c2.getTime();

		int dc = (int) ((endDate2.getTime() - endDate.getTime()) / (1000 * 60 * 60 * 24));// 日期差45天

		List<Date> list = new ArrayList<Date>();

		Date ced = null;
		for (int i = 0; i < dc; i++) {
			Calendar ce = Calendar.getInstance();
			ce.setTime(endDate);
			ce.set(Calendar.DATE, ce.get(Calendar.DATE) + i);
			ced = ce.getTime();
			list.add(ced);
		}
		return list;
	}

	/**
	 * 输入天数，返回现在时间加上输入天数的日期LIST
	 * @param dayNum
	 * @return
	 */
	private static List<Date> getDateList(int dayNum) {
		Date nowDate = new Date();
		Calendar c = Calendar.getInstance();
		c.setTime(nowDate);
		c.set(Calendar.DATE, c.get(Calendar.DATE) + dayNum);
		Date endDate = c.getTime();
		int dc = (int) ((endDate.getTime() - nowDate.getTime()) / (1000 * 60 * 60 * 24));// 日期差45天
		List<Date> list = new ArrayList<Date>();
		Date ced = null;
		for (int i = 0; i < dayNum; i++) {
			Calendar ce = Calendar.getInstance();
			ce.setTime(nowDate);
			ce.set(Calendar.DATE, ce.get(Calendar.DATE) + i);
			ced = ce.getTime();
			list.add(ced);
		}
		return list;
	}

	/**
	 *  处理日期范围返回日期范围7天为一组LIST
	 * @return
	 */
	public List<String> getWeekDate() {
		DateFormat df = new SimpleDateFormat("MM/dd");
		int dayNum = 0;
		if (StringUtils.isBlank(this.sope_Time)
				|| this.sope_Time.equalsIgnoreCase("0")) {
			this.sope_Time = "1";
		}
		if (StringUtils.isNotBlank(this.sope_Time)) {
			this.sope_Time.replace(" ", "");
			if (this.sope_Time.equalsIgnoreCase("1")) {
				dayNum = 7;
			}
			if (this.sope_Time.equalsIgnoreCase("2")) {
				dayNum = 13;
			}
			if (this.sope_Time.equalsIgnoreCase("3")) {
				dayNum = 19;
			}
			if (this.sope_Time.equalsIgnoreCase("4")) {
				dayNum = 25;
			}
			if (this.sope_Time.equalsIgnoreCase("5")) {
				dayNum = 31;
			}
			if (this.sope_Time.equalsIgnoreCase("6")) {
				dayNum = 37;
			}
			if (this.sope_Time.equalsIgnoreCase("7")) {
				dayNum = 42;
			}
			if (this.sope_Time.equalsIgnoreCase("8")) {
				dayNum = 45;
			}
		}
		List<Date> listdate = getDateListByWeek(dayNum);
		for (int i = 0; i < listdate.size(); i++) {
			this.list_str.add(df.format(listdate.get(i)) + "<br/>"
					+ new SimpleDateFormat("EEEE").format(listdate.get(i)));
		}
		return this.list_str;
	}
}
